package base

import (
	"strconv"
)

//Employee 员工
type Employee struct {
	hkey  string
	hkey2 string
	hkey3 string
	hkey4 string
}

var emplCache *Employee

//GetEmployeeCache 获取员工
func GetEmployeeCache() (e *Employee) {
	if emplCache == nil {
		emplCache = &Employee{
			hkey:  "employee",
			hkey2: "emplOrgan",
			hkey3: "employee:no",
			hkey4: "employee:no:id",
		}
	}
	return emplCache
}

//Build 构建
func (e *Employee) Build() error {
	Logger.Info("开始构建员工")
	sql := "select `ID`,`SysUserName`,`OrganID`,`SysUserID` from `T_SysUser`"
	rows, err := MYDB.Raw(sql).Rows()
	if err != nil {
		return err
	}
	defer rows.Close()
	for rows.Next() {
		var id int
		var name string
		var organID int
		var no string
		err := rows.Scan(&id, &name, &organID, &no)
		if err != nil {
			return err
		}
		if err := e.addData(id, name, organID, no); err != nil {
			return err
		}
	}
	Logger.Info("构建员工完成")
	return nil
}
func (e *Employee) addData(id int, name string, organID int, no string) error {
	if err := RedisCli.HSet(rctx, e.hkey, id, name).Err(); err != nil {
		return err
	}
	if err := RedisCli.HSet(rctx, e.hkey2, id, organID).Err(); err != nil {
		return err
	}
	if err := RedisCli.HSet(rctx, e.hkey3, id, no).Err(); err != nil {
		return err
	}
	if err := RedisCli.HSet(rctx, e.hkey4, no, id).Err(); err != nil {
		return err
	}
	return nil
}

//CacheUser 缓存员工
func CacheUser(id int, name string, organID int, no string) error {
	return GetEmployeeCache().addData(id, name, organID, no)
}

//GetNameByID 根据ID获取Name
func (e *Employee) GetNameByID(id int) (string, error) {
	name, err := RedisCli.HGet(rctx, e.hkey, strconv.Itoa(id)).Result()
	if err != nil {
		return "", err
	}
	return name, nil
}

//GetNoByID 根据ID获取No
func (e *Employee) GetNoByID(id int) (string, error) {
	no, err := RedisCli.HGet(rctx, e.hkey3, strconv.Itoa(id)).Result()
	if err != nil {
		return "", err
	}
	return no, nil
}

//GetNoByID 根据ID获取No
func (e *Employee) GetIdByNo(no string) (int, error) {
	idStr, err := RedisCli.HGet(rctx, e.hkey4, no).Result()
	if err != nil {
		return 0, err
	}
	return strconv.Atoi(idStr)
}

//GetNameByNo 根据NO获取Name
func (e *Employee) GetNameByNo(no string) (string, error) {
	id, err := e.GetIdByNo(no)
	if err != nil {
		return "", err
	}
	return e.GetNameByID(id)
}

//GetDeptIDByID 根据ID获取部门ID
func (e *Employee) GetDeptIDByID(id int) (int, int, error) {
	organIDStr, err := RedisCli.HGet(rctx, e.hkey2, strconv.Itoa(id)).Result()
	if err != nil {
		return 0, 0, err
	}
	oID, err := strconv.Atoi(organIDStr)
	if err != nil {
		return 0, 0, err
	}
	opid, err := GetDepartmentCache().GetPOrgIdByID(oID)
	if err != nil {
		return 0, 0, err
	}
	return oID, opid, nil
}

//GetDeptNameByID 根据ID获取部门Name
func (e *Employee) GetDeptNameByID(id int) (string, error) {
	organIDStr, err := RedisCli.HGet(rctx, e.hkey2, strconv.Itoa(id)).Result()
	if err != nil {
		return "", err
	}
	oID, err := strconv.Atoi(organIDStr)
	if err != nil {
		return "", err
	}
	name, err := GetDepartmentCache().GetNameByID(oID)
	if err != nil {
		return "", err
	}
	return name, nil
}
